﻿#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System.Net.Sockets;
using System.Text;

#endregion

namespace ZO.SmartCore.Net.Sockets
{ /// <summary>
    /// Provides a reference to a data segment where data is sent or received over a TCP stream. 
    /// </summary>
    /// <remarks>
    /// When data is sent or received when using the Tcp 
    /// (which may be by using the Tcp directly, or by using another component that 
    /// uses the Tcp for the underlying connection), a Segment object is returned. 
    /// This Segment oject will contain data such as the data sent or received and 
    /// the amount of bytes sent or received. To use the data, 
    /// simply access the Segment.Buffer property using the offset 
    /// specified by the Segment.Offset property. As a shortcut, 
    /// the Segment.ToString method can be used to simply return all data sent or 
    /// received as a string.
    /// </remarks>
    public class Segment 
    {

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="Segment"/> class.
        /// </summary>
        /// <param name="buffer">The buffer containing the data that was sent/received.</param>
        /// <param name="offset">The offset into the buffer.</param>
        /// <param name="count">The amount of bytes that were sent/received.</param>
        /// <param name="encoding">The encoding used to convert string.</param>
        /// <param name="flags">the SocketFlags associated with the Socket. </param>
        public Segment(byte[] buffer, int offset, int count,  
            Encoding encoding, SocketFlags flags) : 
            this(buffer, offset, count, encoding, flags, -1)
        {
    
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Segment"/> class.
        /// </summary>
        /// <param name="buffer">The buffer containing the data that was sent/received.</param>
        /// <param name="offset">The offset into the buffer.</param>
        /// <param name="count">The amount of bytes that were sent/received.</param>
        /// <param name="encoding">The encoding used to convert string.</param>
        /// <param name="flags">the SocketFlags associated with the Socket.</param>
        /// <param name="result">The result.</param>
        public Segment(byte[] buffer, int offset, int count,  
            Encoding encoding, SocketFlags flags, int result)
        {
            this.Buffer = buffer;
            this.Offset = offset;
            this.Count = count;

            if (encoding != null)
            {
                this.Encoding = encoding;
            }
            this.Flags = flags;
        
            if (this.Result > -1)
	        {
                this.Result = result;
            }
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="Segment"/> class.
        /// </summary>
        /// <param name="buffer">The buffer containing the data that was sent/received.</param>
        /// <param name="offset">The offset into the buffer.</param>
        /// <param name="count">The amount of bytes that were sent/received.</param>
        /// <param name="flags">the SocketFlags associated with the Socket. </param>
        public Segment(byte[] buffer, int offset, int count, 
            SocketFlags flags) : this(buffer, offset, count, null, flags) { }


        /// <summary>
        /// Initializes a new instance of the <see cref="Segment"/> class.
        /// </summary>
        /// <param name="buffer">The buffer containing the data that was sent/received.</param>
        /// <param name="offset">The offset into the buffer.</param>
        /// <param name="count">The amount of bytes that were sent/received.</param>
        /// <param name="flags">the SocketFlags associated with the Socket.</param>
        /// <param name="result">The result.</param>
        public Segment(byte[] buffer, int offset, int count,
            SocketFlags flags, int result)
            : this(buffer, offset, count, null, flags, result) { }
        #endregion

        #region Destructor

        #endregion

        #region Fields
        private byte[] _Buffer;
        private int _Offset;
        private int _Count;
        private Encoding _Encoding = Encoding.Default;

        private SocketFlags _Flags;

        private int _Result = -1;
        #endregion

        #region Events

        #endregion

        #region Operators
  
        #endregion

        #region Properties
        /// <summary>
        /// The buffer containing the data that was sent/received.
        /// </summary>
        /// <value>A byte array containing data that was sent/received over the socket.</value>
        public byte[] Buffer
        {
            get
            {
                return this._Buffer;
            }
            set
            {
                _Buffer = value;

                if (value != null)
                {
                    this.Count = this._Buffer.Length;
                    this.Offset = 0;
                }
                else
                {
                    this.Count = 0;
                    this.Offset = 0;
                }

            }

        }

        /// <summary>
        /// The amount of bytes that were sent/received. 
        /// </summary>
        /// <value>An integer value representing the amount of bytes that were sent/received.</value>
        public int Count
        {
            get
            {
                return this._Count;
            }
            set
            {
                _Count = value;
            }
        }

        /// <summary>
        /// The offset into the buffer. 
        /// </summary>
        /// <value>An integer representing the offset into the buffer.</value>
        public int Offset
        {
            get
            {
                return this._Offset;
            }
            set
            {
                _Offset = value;
            }
        }
        
        /// <summary>
        /// Gets the SocketFlags associated with the Socket. 
        /// </summary>
        /// <value>The SocketFlags associated with the Socket.</value>
        public SocketFlags Flags
        {
            get
            {
                return this._Flags;
            }
            set
            {
                _Flags = value;
            }
        }

        /// <summary>
        /// Gets or sets the encoding.
        /// </summary>
        /// <value>The encoding.</value>
        public Encoding Encoding
        {
            get { return _Encoding; }
            set { _Encoding = value; }
        }

        /// <summary>
        /// The Result of sent/received operation.
        /// </summary>
        /// <remarks>It may be -1 if operation is async or any error ocuured in operation</remarks>
        public int Result
        {
            get
            {
                return this._Result;
            }
            set
            {
                _Result = value;
            }

        }
        #endregion

        #region Methods
        /// <summary>
        /// Returns a <see cref="T:System.String"></see> that represents the buffer>.
        /// </summary>
        public override string ToString()
        {
            if (Result > 0)
            {
                return this.Encoding.GetString(this.Buffer, this.Offset, this.Result);
            }
            return this.Encoding.GetString(this.Buffer, this.Offset, this.Count);
        }

  
        #endregion

    

     

    }
}
